<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Material extends Model
{
    /**
     * 可批量赋值的属性
     */
    protected $fillable = [
        'name',
        'type_id',
        'unit',
        'quantity',
        'purpose',
        'remark'
    ];

    /**
     * 获取该材料所属的类型
     */
    public function type()
    {
        return $this->belongsTo(Type::class);
    }

    /**
     * 获取该材料的出入库记录
     */
    public function inventoryRecords()
    {
        return $this->hasMany(InventoryRecord::class);
    }

    /**
     * 获取库存状态
     */
    public function getStockStatusAttribute()
    {
        if ($this->quantity <= 0) {
            return 'out_of_stock';
        } elseif ($this->quantity <= 10) {
            return 'low_stock';
        } else {
            return 'normal';
        }
    }

    /**
     * 获取库存状态文本
     */
    public function getStockStatusTextAttribute()
    {
        $status = [
            'out_of_stock' => '无库存',
            'low_stock' => '库存不足',
            'normal' => '正常'
        ];

        return $status[$this->stock_status] ?? '未知';
    }

    /**
     * 获取库存状态样式
     */
    public function getStockStatusClassAttribute()
    {
        $classes = [
            'out_of_stock' => 'danger',
            'low_stock' => 'warning',
            'normal' => 'success'
        ];

        return $classes[$this->stock_status] ?? 'secondary';
    }
} 